﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace CloudSharingStation_UWP_.Model
{
    class Account
    {
        public string name { get; set; }
        public string token { get; set; }
        public string password { get; set; }
        public int id { get; set; }

        public Account(string name, string password)
        {
            this.name = name;
            this.password = password;
        }

        /* 定义post以及get方法 */
        /// <summary> 发送登录的Http请求
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Dictionary<HttpStatusCode, string> Login()
        {
            string loginUrl = "https://localhost:" + Config.localNum + "/api/user/login"; // 登录的URL
            Dictionary<string, object> parameters = new Dictionary<string, object>();

            parameters.Add("name", this.name);
            parameters.Add("pwd", this.password);
            StringBuilder builder = new StringBuilder();
            Dictionary<HttpStatusCode, string> resdic = new Dictionary<HttpStatusCode, string>();
            builder.Append(loginUrl);
            if (parameters.Count > 0)
            {
                builder.Append("?");
                int i = 0;
                foreach (var item in parameters)
                {
                    if (i > 0)
                        builder.Append("&");
                    builder.AppendFormat("{0}={1}", item.Key, item.Value);
                    i++;
                }
            }
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(Config.CheckValidationResult);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
            req.ProtocolVersion = HttpVersion.Version10;
            req.Method = "GET";
            req.ContentType = "application/x-www-form-urlencoded";
            req.UserAgent = Config.DefaultUserAgent;
            //添加参数
            try
            {
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                string token;
                Stream stream = resp.GetResponseStream();
                try
                {
                    //获取内容
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        token = reader.ReadToEnd();
                    }
                }
                finally
                {
                    stream.Close();
                }
                resdic.Add(resp.StatusCode, token);
                return resdic; // 返回登陆状态码以及token
            }
            catch (System.Net.WebException exception)
            {
                Console.WriteLine(exception.Message);
                Dictionary<HttpStatusCode, string> ResErrorDic = new Dictionary<HttpStatusCode, string>();
                ResErrorDic.Add(HttpStatusCode.BadRequest, null); // 根据web错误返回值
                return ResErrorDic; // 返回错误字典
            }
        }

    }
}
